$(function() {
	var zIndex = 1000;
	var outerHtmls = {};
	var Dlg = function(element, options) {
		this.element = element;
		if(options) {
			this.emptyClose = options.emptyClose || false;
			this.showClose = options.showClose || false;
			this.btns = options.btns || [];
			this.btnsClickCallBack = options.btnsClickCallBack || function() {};
			this.title = options.title;
			this.complete = options.complete || function() {};
		}
		this.dlgId = 0;
		
		this.init();
	}
	Dlg.prototype.init = function() {
		if(this.element.parents(".dlg-wrap").length > 0){
			return false;
		}
		var self = this;
		var randomNum = parseInt(Math.random() * 100000);
		var outerHtml = this.element.get(0).outerHTML;
		var k = {};
		k.obj = self;
		k.orginHTML = outerHtml;
		k.element = self.element;
		outerHtmls[randomNum] = k; 
		
		
		var realHtml = "<div class=\"dlg-wrap\" style=\"z-index:" + (++zIndex) + "\" id=\"dlg_" + randomNum + "\"><div class=\"dlg-zz\"><div class=\"dlg-bk\">" + this.getTitleHTML() + this.getCloseHTML();
		realHtml += "<div class=\"dlg-content\">" + outerHtml + "</div>" + this.btnHtml() + "</div></div></div>";
		this.element.remove();
		$("body").append(realHtml)
		this.dlgId = randomNum;
		this.bindEvent();
		$("#dlg_" + this.dlgId).find(".dlg-content").children().show();
		$("#dlg_" + this.dlgId).fadeIn('fast');
		this.complete();
	}
	Dlg.prototype.btnHtml = function() {
		if(this.btns) {
			var bHtml = "<div class=\"dlg-btns\">";
			$(this.btns).each(function(index, item) {
				bHtml += "<button class=\"dlg-btn-" + index + "\" >" + item + "</button>";
			})
			bHtml += "</div>"
			return bHtml;
		} else {
			return "";
		}
	}
	Dlg.prototype.getTitle = function() {
		return this.title || "";
	}
	Dlg.prototype.setTitle = function(title) {
		this.title = title;
	}
	Dlg.prototype.getTitleHTML = function() {
		if(this.getTitle || this.title != "") {
			return "<div class=\"dlg-title\">" + this.getTitle() + "</div>";
		}
		return "";
	}
	Dlg.prototype.getCloseHTML = function() {
		if(this.showClose) {
			return "<div class=\"dlg-close\">×</div>";
		}
		return "";
	}
	
	Dlg.prototype.getDlgId = function() {
		return this.dlgId;
	}
	Dlg.prototype.close = function(o) {
		var self = this;
		var dlgId = (o?o:self.dlgId);
		$("#dlg_" + dlgId).fadeOut('fast', function() {
			$("#dlg_" + dlgId).find(".dlg").hide();
			$("body").append(outerHtmls[dlgId].orginHTML)
			$("#dlg_" + dlgId).remove()
			delete outerHtmls[dlgId];
		});
	}
	Dlg.prototype.closeAll = function() {
		var self = this;
		for(var o in outerHtmls) {
			self.close(o);
		}
	}

	Dlg.prototype.bindEvent = function() {
		var self = this;
		$("body").on("click", "#dlg_" + this.dlgId + " .dlg-btns button", function() {
			self.btnsClickCallBack($(this).index());
		})

		$("body").on("click", "#dlg_" + this.dlgId + " .dlg-close", function() {
			self.close()
		});
		if(self.emptyClose) {
			$("body").on("click", "#dlg_" + this.dlgId + " .dlg-zz", function() {
				var div = $(this).find(".dlg-bk").get(0);
				var x = event.clientX;
				var y = event.clientY;
				var divx1 = div.offsetLeft;
				var divy1 = div.offsetTop;
				var divx2 = div.offsetLeft + div.offsetWidth;
				var divy2 = div.offsetTop + div.offsetHeight;
				if(x < divx1 || x > divx2 || y < divy1 || y > divy2) {
					self.close()
				}
			});
		}
	}

	$.fn.extend({
		Dialog: function(options) {
			return new Dlg($(this), options)
		}
	})
	
}(jQuery))
function showLoading(msg){
	var hideBg="<div class=\"dlg-wrap dlg-loading\" style=\"z-index:10000;\">";
	hideBg += "<div class=\"dlg-zz\"><div class=\"dlg-bk\">";
	hideBg += "<img src=\"/img/dlg-loading.gif\">"
	hideBg += "<div class=\"dlg-loading-txt\">";
	hideBg += msg||"正在加载";
	hideBg +="</div></div></div></div>";
	$("body").append(hideBg);
	$(".dlg-wrap").show();
}

function closeLoading(){
	$(".dlg-loading").remove();
}

function dlgConfirm(msg, success) {
	var confirmClass = "confirm" + parseInt(Math.random() * 100000);
	var hideBg = "<div class=\"dlg " + confirmClass + "\"><div class=\"dlg-bk\">";
	hideBg += msg;
	hideBg += "</div></div>";
	$("body").append(hideBg);
	var d = $("." + confirmClass).Dialog({
		title: "确认提示",
		showClose: true,
		emptyClose: false,
		btns: ["确定", "取消"],
		btnsClickCallBack: function(i) {
			if(i == 0) {
				if(success){
					success();
					d.close();
					$("." + confirmClass).remove();
				}
			} else if(i == 1) {
				d.close();
				$("." + confirmClass).remove();
			}
		}
	});
}
function dlgAlert(msg, success, complete){
	var alertClass = "alert" + parseInt(Math.random() * 100000);
	var hideBg = "<div class=\"dlg " + alertClass + "\"><div class=\"dlg-bk\">";
	hideBg += msg;
	hideBg += "</div></div>";
	$("body").append(hideBg);
	var d = $("." + alertClass).Dialog({
		title: "提示",
		showClose: true,
		emptyClose: false,
		btns: ["确定"],
		btnsClickCallBack: function(i) {
			d.close();
			$("." + alertClass).remove();
			if(success){
				success();
			}
		},
		complete: function() {
			if(complete){
				complete();
			}
		}
	});
}
